<template>
  <div class="edit-gender">
    <van-picker
      ref="picker"
      title="修改性别"
      show-toolbar
      :default-index="defaultIndex"
      :columns="columns"
      @confirm="onConfirm"
      @cancel="onCancel"
    />
  </div>
</template>

<script>
// 修改个人资料接口
import { modifyProfile } from '@/api/userInfo.js'
export default {
  name: 'EditUserName',
  data() {
    return {
      columns: ['男', '女']
    }
  },
  created() {},
  props: {
    gender: {
      type: Number,
      required: true
    }
  },
  methods: {
    async onConfirm(value, index) {
      try {
        const data = {
          gender: index
        }
        await modifyProfile(data)
        this.$emit('sure', value)
        this.$emit('close')
        this.$toast.success('修改成功')
      } catch (error) {
        this.$toast.fail('请稍后再试')
      }
    },
    onCancel() {
      this.$emit('close')
      // 利用ref调用vant组件的方法
      this.$refs.picker.setColumnValue(0, this.columns[this.gender])
    }
  },
  computed: {
    defaultIndex() {
      return this.columns.findIndex((item) => {
        const userOriginGender = this.gender ? '女' : '男'
        return item === userOriginGender
      })
    }
  },
  watch: {},
  components: {}
}
</script>

<style lang="less" scoped>
</style>
